<template>
	<!-- 运营系统-短剧配置 -->
	<d2-container class="d-flex flex-column">
		<template v-if="$hasPermission('playlet:appletCoupon:index')">
			<div class="d-flex j-sb" slot="header">
				<el-form class="d-flex b-s mt-2 mb-10" :inline="true" size="small" :model="dataForm"
					@keyup.enter.native="getTableList(true)" @submit.native.prevent>
					<el-form-item label="券名称">
						<el-input v-model="dataForm.keyword" placeholder="券名称" clearable />
					</el-form-item>
					<el-form-item>
						<el-button icon="el-icon-search" type="primary"
							@click="getTableList(true)">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="success" icon="el-icon-circle-plus-outline" v-show="$hasPermission('playlet:appletCoupon:create')"
							@click="addOrUpdateData()">新增</el-button>
					</el-form-item>
				</el-form>
				<div class="d-flex">
					<vxe-toolbar ref="toolbarRef" custom :refresh="{ queryMethod: getTableList,code:'query' }"></vxe-toolbar>
				</div>
			</div>
			<div class="d-flex h-100 b-s flex-column">
				<div class="d-flex flex-1">
					<div class="overflow">
						<vxe-table ref="dataBackhaulSettingTable" key="dataBackhaulSettingTable" border height="100%" resizable size="medium"
							:loading="tableLoading" :loading-config="{icon: 'vxe-icon-indicator roll', text: '正在拼命加载中...'}"
							:data="dataList">
							<vxe-column width="100" field="id" title="ID" sortable align="center"></vxe-column>
							<vxe-column width="260" field="name" title="券名称" align="center"></vxe-column>
							<vxe-column width="260" field="applet_name" title="小程序名称" align="center"></vxe-column>
							<vxe-column width="130" field="stock_number" title="库存量" align="center"></vxe-column>
							<vxe-column width="180" field="receive_begin_time" title="生效时间" sortable cell-type="string" align="center"></vxe-column>
							<vxe-column width="180" field="receive_end_time" title="过期时间" sortable cell-type="string" align="center"></vxe-column>
							<vxe-column width="260" field="consume_desc" title="使用须知" align="center"></vxe-column>
							<vxe-column width="180" field="created_at" title="创建时间" sortable cell-type="string" align="center"></vxe-column>
							<vxe-column width="180" field="updated_at" title="修改时间" sortable cell-type="string" align="center"></vxe-column>
							<vxe-column v-if="!tableLoading||(dataList.length&&$hasPermissionNum(['playlet:appletCoupon:update','playlet:appletCoupon:delete','playlet:appletCoupon:updateMetaStatus','playlet:appletCoupon:updateMetaStock']))"
							title="操作" fixed="right" :width="$hasPermissionNum(['playlet:appletCoupon:update','playlet:appletCoupon:delete','playlet:appletCoupon:updateMetaStatus','playlet:appletCoupon:updateMetaStock'])*100" align="center" cell-class-name="py-10">
								<template v-slot="{ row }">
									<el-button size="small" round icon="el-icon-edit"
									v-if="$hasPermission('playlet:appletCoupon:update')"
									@click="addOrUpdateData(row)" type="primary">编辑</el-button>
									<el-button size="small" round icon="el-icon-edit"
									v-if="$hasPermission('playlet:appletCoupon:updateMetaStock')"
									@click="dialogFormFn(row)" type="primary">修改库存</el-button>
									<el-button size="small" round icon="el-icon-edit"
									v-if="$hasPermission('playlet:appletCoupon:updateMetaStatus') && row.status != 1"
									@click="updateMetaStatusFn(row)" type="success">上架</el-button>
									<el-button size="small" round icon="el-icon-edit"
									v-if="$hasPermission('playlet:appletCoupon:updateMetaStatus') && row.status == 1"
									@click="updateMetaStatusFn(row)" type="warning">下架</el-button>
									<el-button size="small" round icon="el-icon-delete"
									v-if="$hasPermission('playlet:appletCoupon:delete')"
									@click="deleteHandleSetter(row)" type="danger">删除</el-button>
								</template>
							</vxe-column>
							<template #empty>
								<div class="d-flex flex-row a-center j-center font-md">
									<div class="d-flex">
										暂时没有数据~
									</div>
								</div>
							</template>
						</vxe-table>
					</div>
				</div>
			</div>
			<!-- 分页 -->
			<div class="d-flex" slot="footer">
				<vxe-pager background size="medium" align="right" :border="false" :pageSizes="[20,50,100,150,200]"
					:current-page="dataForm.page" :page-size="dataForm.pageSize" :total="total"  :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes', 'FullJump', 'Total']"
					@page-change="handlePageChange">
				</vxe-pager>
			</div>
			<!-- 新增/修改弹窗 -->
			<appletCoupon-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateDialog" @refreshDataList="getTableList"></appletCoupon-add-or-update>

			<!-- 修改库存 -->
			<el-dialog title="修改库存" :visible.sync="dialogFormVisible" width="30%">
				<el-form :model="visibleData">
					<div style="font-size: 16px;color: #000;">券基础信息</div>
				
					<div style="padding: 22px;background: #f1f1f1;margin: 12px 0 22px 0;border-radius: 4px;">
						<div style="margin-bottom: 20px;display: flex;">
							<div style="width: 35%;color: rgb(51 49 49);">券名称</div> <div style="width: 65%;">{{visibleData.name}}</div>
						</div>
						<div style="margin-bottom: 20px;display: flex;">
							<div style="width: 35%;color: rgb(51 49 49);">券ID</div> <div>{{visibleData.id}}</div>
						</div>
						<div style="margin-bottom: 20px;display: flex;">
							<div style="width: 35%;color: rgb(51 49 49);">第三方关联券ID</div> <div>{{visibleData.coupon_meta_id}}</div>
						</div>
						<div style="margin-bottom: 20px;display: flex;">
							<div style="width: 35%;color: rgb(51 49 49);">当前券库存</div> <div>{{visibleData.stock_number}}</div>
						</div>
					</div>
					<div style="font-size: 16px;color: #000;">增加张数</div>
				
				
				</el-form-item>
					<div style="color: rgb(51 49 49);margin-top: 20px;">
						<span style="margin-right: 6px;">增加</span><el-input-number v-model="visibleData.addstock_number" :min="0"  label="描述文字"></el-input-number>
						<span style="margin-left: 6px;">张</span>
					</div>
				</el-form>

				<div slot="footer" class="dialog-footer">
					<el-button @click="dialogFormVisible = false">取 消</el-button>
					<el-button type="primary" @click="dialogFormVisibleFN()">确 定</el-button>
				</div>
			</el-dialog>
		</template>
		<template v-else>
			<div class="d-flex a-center j-center h-100 b-s flex-column">
				<img class="d-flex b-s mt-2 mb-10" src="../../../public/image/download.png" alt="没有访问权限" style="width: 300px;">
				<div class="d-flex">抱歉，您没有访问权限</div>
			</div>
		</template>
	</d2-container>
</template>

<script>
	import mixinViewModule from '@/mixins/view-module'
	import appletCouponAddOrUpdate from './appletCoupon-add-or-update'
	export default {
		mixins: [mixinViewModule],
		components: {
			appletCouponAddOrUpdate
		},
		data() {
			return {
				visibleData:{
					addstock_number:0,
				},
				
				dialogFormVisible:false,
				tableLoading:false,
				total:0,
				// 增改
				addOrUpdateVisible: false,
				dataList:[],
				dataForm: {
					page:1,
					pageSize:20,
					keyword: ''

				}
			}
		},
		created() {
			if(this.$hasPermission('playlet:appletCoupon:index')){
				this.getTableList();
				// this.getTableList2();
			}
		},
		methods: {
			
			async getTableList(init=false){
				if(init&&typeof init =='boolean'){
					this.dataForm.page = 1;
				}
				this.tableLoading = true;
				const tableRes = await this.$axios.get('/api/playlet/appletCoupon?'+this.$qs.stringify(this.dataForm))
				this.tableLoading = false;
				this.dataList = tableRes.data;
				this.total = tableRes.total;
			},


			//新增/修改弹窗
			async addOrUpdateData(row) {
				console.log(row)
				this.addOrUpdateVisible = true;
				if (typeof row == 'object' && row!=null) {
					const id = row.id;
					const userRes = await this.$axios.get(`/api/playlet/appletCoupon/update/${id}`,{});
					this.$nextTick(() => {
						this.$refs.addOrUpdateDialog.id = id;
						this.$refs.addOrUpdateDialog.update(userRes);
					})
				} else {
					this.$nextTick(() => {
						this.$refs.addOrUpdateDialog.id = null;
						this.$refs.addOrUpdateDialog.init()
					})
				}
			},
			dialogFormVisibleFN(){
				console.log(184,this.visibleData)
				this.$axios.post(`/api/playlet/appletCoupon/updateMetaStock/${this.visibleData.id}`,{add_number:this.visibleData.addstock_number});
				this.dialogFormVisible = false
				this.$message({
							message: this.$t('views.public.success'),
							type: 'success',
							duration: 500,
							onClose: () => {
								this.visible = false;
								this.$emit('refreshDataList');
							}
						})
			},
			async dialogFormFn(row){
				this.addOrUpdateVisible = true;
				if (typeof row == 'object' && row!=null) {
					const id = row.id;
					const userRes = await this.$axios.get(`/api/playlet/appletCoupon/updateMetaStock/${id}`,{});
					console.log(185,userRes)
					this.$nextTick(() => {
						this.dialogFormVisible = true
						this.visibleData = {
							addstock_number:0,
							...userRes
						}
						// this.$refs.addOrUpdateDialog.id = id;
						// this.$refs.addOrUpdateDialog.update(userRes);
					})
				}
			},
			updateMetaStatusFn(row){
				console.log(221,row.status )
				
						this.$confirm(this.$t('public.prompt.info', {
						handle: row.status==1?'下架':'上架'
					}), this.$t('public.prompt.title'), {
						confirmButtonText: this.$t('views.public.confirm'),
						cancelButtonText: this.$t('views.public.cancel'),
						type: 'warning'
					}).then(() => {
						this.$axios.post(`/api/playlet/appletCoupon/updateMetaStatus/${row.id}`,{status:row.status==1?2:1}
						).then(res => {
							this.$message({
								message: this.$t('views.public.success'),
								type: 'success',
								duration: 500,
								onClose: () => {
									this.getTableList()
								}
							})
						}).catch(() => {})
					}).catch(() => {})
			
			},
			// 删除
			deleteHandleSetter(row) {
				this.$confirm(this.$t('public.prompt.info', {
					handle: this.$t('views.public.delete')
				}), this.$t('public.prompt.title'), {
					confirmButtonText: this.$t('views.public.confirm'),
					cancelButtonText: this.$t('views.public.cancel'),
					type: 'warning'
				}).then(() => {
					this.$axios.delete(`/api/playlet/appletCoupon/delete/${row.id}`,{}
					).then(res => {
						this.$message({
							message: this.$t('views.public.success'),
							type: 'success',
							duration: 500,
							onClose: () => {
								this.getTableList()
							}
						})
					}).catch(() => {})
				}).catch(() => {})
			}
		}
	}
</script>

<style>

</style>